package com.shujia.dao;

import com.shujia.entity.Dept;
import com.shujia.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

//@Service
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Dept> getAllCusBaseInfo() {
        //查询部门信息
        List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from dept");
        List<Dept> cusBaseInfoList=new ArrayList<>();
        for (Map<String,Object> map:list ) {
            Dept cusBaseInfo=new Dept();
            //部门id
            cusBaseInfo.setDeptno(map.get("deptno").toString());

            cusBaseInfo.setDname(map.get("dname").toString());

            cusBaseInfo.setLoc(map.get("loc").toString());
            cusBaseInfoList.add(cusBaseInfo);
        }
        return cusBaseInfoList;
    }

    @Override
    public Dept saveDeptInfo(Dept dept) {
        dept.setDeptno(UUID.randomUUID().toString());
        String sql="insert into deptno(deptno,dname,loc) values(?,?,?)";
        jdbcTemplate.update(sql,new Object[]{dept.getDeptno(),
                dept.getDname(),
                dept.getLoc(),},new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR});
        //获取信息
        return getDeptByNo(dept.getDeptno());
    }

    @Override
    public Dept getDeptByNo(String deptNo) {
        Map<String,Object> map=jdbcTemplate.queryForMap("select * from cus_base_info where cus_id = ?",deptNo);
        Dept cusBaseInfo=new Dept();

        cusBaseInfo.setDeptno(map.get("deptno").toString());

        cusBaseInfo.setDname(map.get("dname").toString());

        cusBaseInfo.setLoc(map.get("loc").toString());
        return cusBaseInfo;
    }
}
